<?php
global $AppUI, $m, $tab;
require_once($AppUI->getModuleClass('quotations'));
// Get login object
$uid = $AppUI->user_id;
// Check permission
// TODO: Check permission role
// Check edit
$perms =& $AppUI->acl();
$canAccess = $perms->checkModuleItem('quotations', 'access');
$canEdit = $perms->checkModuleItem('quotations', 'edit');
$canDelete = $perms->checkModuleItem('quotations', 'delete');

// check permission list quotations
if(!$canAccess){
	$AppUI->redirect(PUBLIC_PAGE);
}

// Status scope
// Include mdstatus
require_once($AppUI->getMasterDataClass('mdstatus'));
// Get active record
$active_key = dPgetSysVal(ACTIVE_KEY);
$objMD = new CMdStatus();
$objMD->loadByCode($active_key['code']);
$act_status_id = $objMD->status_id;
// Get complete record
$complete_key = dPgetSysVal(COMPLETED_KEY);
$objMD->loadByCode($complete_key['code']);
$complete_status_id = $objMD->status_id;

$confOther = -1;
// Load Status list
$objMD = new CMdStatus();
$statList = $objMD->getStatusList('Quotation');
// Init variable index
$confNum = 0;

// Quotation scope
// Prefix of quotations
$prefix = 'quote_';
// Quotation's path
$pathRoot = './index.php?m=quotations';
$pathEditQuote = $pathRoot.'&a=quote_crud';
$pathViewQuote = $pathRoot.'&a=quote_view';
$pathCompanyView = './index.php?m=companies&a=view';

// Load Object Status
$objMD = new CMdStatus();
$statList = $objMD->getStatusList('Quotation');
$quote_status = intval( dPgetParam( $_POST, 'quote_status', 0 ) );
/*
 * Business logic
 *
 */
//$page_offset = intval(dPgetParam($_GET, 'page_offset', 0));
//$page_limit = intval(dPgetParam($_POST, 'page_limit', 10));
// Condition search
$extra = '';
$keyword = dPgetParam($_POST, 'keyword', '');
if($keyword != ''){
	$extra['where'] = "quote_code like '%". $keyword . "%'";
}
if ($quote_status != 0) {
	if(isset($extra['where']) and $extra['where'] != ""){
		$extra['where'] .= " AND quote_status = $quote_status";	
	}
	else{
		$extra['where'] = " quote_status = $quote_status";
	}
}
// Get instance Quotation;
$obj = new CQuotations();
$status = dPgetParam($_GET, 'quote_status', 0);
if(intval($status) < 0){
	$extra['where'] = 'quote_status <> ' . $act_status_id;
}

$result = $obj->getListQuotations($uid, $status, $extra);
$final = array();

foreach($result as $item){
	$final[$item[$prefix.'customer']]['name'] = $item['cus_name'];
	$final[$item[$prefix.'customer']]['id'] = $item[$prefix.'customer'];
	$final[$item[$prefix.'customer']]['item_list'][] =
		array('id' => $item[$prefix.'id'], 'code' => $item[$prefix.'code'],
			 //'name' => $item[$prefix.'name'], 'status' => $item[$prefix.'status'],
			 'name' => $item[$prefix.'name'], 'status' => $item['status_name'], 'status_id' => $item[$prefix.'status'],
			 //'type' => $item[$prefix.'type'], 'shipping_type' => $item[$prefix.'shipping_type']);
			 'type' => $item['type_name'], 'shipping_type' => $item['shipping_type_name'],
			 'completed' => $item['quote_completed']);

	if($act_status_id==intval($item[$prefix.'status'])){
		$confNum += 1;
	}
}
?>
<form name="editFrm" action="./index.php?m=quotations" method="post">
<input type="hidden" id="dosql" name="dosql" value="do_quote_list"/>
<table width="100%" cellspacing="0" cellpadding="7" border="0" class="infopanel">
<tbody><tr>
	<td colspan="100%">
		<a href="<?=$pathRoot?>" alt="<?=$AppUI->_('Total')?>"><b><?=$AppUI->_('Total')?></b></a>: <?=sizeof($result)?>&nbsp;&nbsp;&nbsp;
		<a href="<?=$pathRoot.'&quote_status='.$act_status_id?>" alt="<?=$AppUI->_('Comfirmed')?>"><b><?=$AppUI->_('Comfirmed')?></b></a>: <?=$confNum?>&nbsp;&nbsp;&nbsp;
		<a href="<?=$pathRoot.'&quote_status='.$confOther?>" alt="<?=$AppUI->_('Other')?>"><b><?=$AppUI->_('Other')?></b></a>: <?=sizeof($result) - $confNum?>&nbsp;&nbsp;&nbsp;
	</td>	
	<td align="right">
		<b><?=$AppUI->_('Status')?>: </b>
		<?= arraySelect($statList, 'quote_status', ' id="quote_status" size="1" class="text" onchange="this.form.submit();"', $quote_status, true)?>
		<b><?=$AppUI->_('Keyword')?>: </b>
		<input type="textfield" class="text" value="" name="keyword" maxlength="32" size="20"/>
		<input type="button" value="<?=$AppUI->_('Search')?>" name="searchBtn" onclick="return doSearchQuotation(this.form, 'keyword', 'dosql')"/>
	</td>
</tr>
</tbody>
</table>
<table id="idQuoteList" width="100%" cellspacing="0" cellpadding="2" border="0" class="list">
	<tbody>
		<tr>
			<th style="width: 20px;" nowrap="nowrap">&nbsp;</th>
			<th style="width: 20px;"><image src="images/tick.gif" border="0"/></th>
			<th nowrap="nowrap"><?=$AppUI->_('Name')?></th>
			<th nowrap="nowrap"><?=$AppUI->_('Code')?></th>
			<th nowrap="nowrap"><?=$AppUI->_('Status')?></th>
			<th nowrap="nowrap"><?=$AppUI->_('Type')?></th>
			<th nowrap="nowrap"><?=$AppUI->_('Shipping Type')?></th>
			<th style="width: 20px;">&nbsp;</th>
			<!--th style="width: 20px;"><?=$AppUI->_('Set')?></th-->
		</tr>
		<?php if((is_null($final)) or (!is_array($final))):?>
		<tr class="second">
			<td colspan="100%">No item available</td>
		</tr>
		<?php else: ?>
		<?php foreach($final as $item):?>
		<tr>
			<td align="center">
				<a href="#"><img title="show only this company" src="images/icons/collapse.gif"/></a>
			</td>
			<td colspan="9" style="padding: 8px;">
				<a href="<?=$pathCompanyView?>&company_id=<?=$item['id']?>" title="<?=$AppUI->_('Company Detail')?>" style="border: 1px outset rgb(238, 238, 238); background-color: rgb(255, 255, 255); padding: 3px;"><b><?=$item['name']?></b></a>
			</td>
		</tr>
			<?php $index = 0; foreach($item['item_list'] as $subItem):?>
			<?php $index += 1;?>
			<tr class="<?= ($index%2)==0 ? 'first': 'second' ?>">
				<td align="center">
					<?php if($subItem['completed']==NORMAL_VAL and $canEdit):?>
					<a href="<?=$pathEditQuote?>&quote_id=<?=intval($subItem['id'])?>"><img title="<?=$AppUI->_('Edit')?>" src="images/edit.gif" border="0"/></a>
					<?php endif;?>
				</td>
				<td>
					<image src="images/<?=$subItem['completed'] > 0 ? 'tick.gif' : 'bullet.gif'?>" border="0"/>
				</td>
				<td><a href="<?=$pathViewQuote?>&quote_id=<?=intval($subItem['id'])?>"><b><?=$subItem['name']?></b></a></td>
				<td><?=$subItem['code']?></td>
				<td><?=$subItem['status']?></td>
				<td><?=$subItem['type']?></td>
				<td><?=$subItem['shipping_type']?></td>
				<td align="center">
					<?php if($canDelete and ($subItem['status_id'] != $act_status_id) and ($subItem['status_id'] != $complete_status_id)): ?>
					<a href="#" alt="" onclick="return deleteQuotation('<?=$subItem['name']?>', <?=$subItem['id']?>)"><img alt="<?=$AppUI->_('Delete')?>" src="images/delete.gif" border="0"/></a>
					<?php endif; ?>
				</td>
				<!--td algin="center">
					<?php echo $$subItem['status_id']; if($subItem['status_id']!=$act_status_id):?>
					<input type="checkbox" name="quoteIdArr[]" value="<?=$subItem['id']?>"/>
					<?php endif;?>
				</td-->
			</tr>
			<?php endforeach;?>
		<?php endforeach;?>
		<?php endif;?>
		<tr>
			<td colspan="100%" align="right"><!--
				<?php if($canEdit):?>
				<input type="submit" name="updateStatus" value="update Status"/>
				<?= arraySelect($statList, 'quote_status', 'size="1" class="text" style="width: 135px;"', @$row->quote_status, true)?>
				<?php endif;?>-->
			</td>
		</tr>
	</tbody>
</table>
</form>
<form name="MdFormDelete" id="MdFormDelete" action="?m=quotations" method="post">
	<input type="hidden" name="dosql" value ="do_delete" />
	<input type="hidden" id="del_cc_id" name="quote_id" value ="" />
	<input type="hidden" name="del" value ="" />
</form>
<script language="JavaScript" type="text/javascript">
  Table("idQuoteList").setNormalHover(1);
</script>